#include "examples.h"

void runmcLeastC(){
	int m,n,k,state,i;
	double *A, *xorin, *noise, *y, rho;
	OPTS opt;
	double c1,c2;
	stopWatch sw;
	FUNVAL result1,result2,result3;
	m=n=100;
	k=10;
	A= (double *) malloc( sizeof(double) * m* n);
	xorin = (double *) malloc(sizeof(double) * n*k);
	noise = (double *) malloc(sizeof(double) * m*k);
	y = (double *) malloc(sizeof(double) * m*k);

	state = 2344;
	acml_randseed(state);
	acml_randn(A,m*n,0.0,1.0);

	state = 13255;
	acml_randseed(state);
	acml_randn(xorin,n*k,0.0,1.0);

	state = 333;
	acml_randseed(state);
	acml_randn(noise,m*k,0.0,1.0);
	dcopy(m*k,noise,1,y,1);

	dgemm('N','N',m,k,n,1.0,A,m,xorin,n,0.01l,y,m);
	fprintM(C_TMP_DIR "xOrin.mat",xorin,m,1);

	fprintM(C_TMP_DIR "A.matrix",A,m,n);
	fprintM(C_TMP_DIR "y.matrix",y,m,k);

	rho = 50;
	memset(A,0,m*n* sizeof(double));
	memset(y,0,m*k* sizeof(double));
	fscanM(C_TMP_DIR "A.matrix",A,m,n);
	fscanM(C_TMP_DIR "y.matrix",y,m,k);

	initOPTS(&opt,m,n,0);
	opt.mc_k = k;
	opt.q = 2.0;
	opt.init = 2;
	opt.maxIter = 100;
	opt.nFlag = 0;
	opt.rsL2 = 0;
	opt.tFlag = 5;
	opt.rFlag = 0;

//	initNormalization(&opt,A);
    
	opt.mFlag=0;
	opt.lFlag =0;
	startTimer(&sw);
	result1 = mcLeastC(A,y,rho,opt);
	stopTimer(&sw);
	printf("Time taken = %lf\n",getElapsedTime(&sw));

	startTimer(&sw);
	opt.mFlag = 1;
	opt.lFlag = 1;
	result3 = mcLeastC(A,y,rho,opt);
	stopTimer(&sw);
	printf("Time taken = %lf\n",getElapsedTime(&sw));

	fprintM(C_TMP_DIR "mcLeastC\\x1.matrix",result1.x,n,k);
	fprintM(C_TMP_DIR "mcLeastC\\funVal1.list",result1.funVal,opt.maxIter,1);
	fprintM(C_TMP_DIR "mcLeastC\\lVal1.list",result1.ValueL,opt.maxIter,1);
	free(result1.x);
	free(result1.funVal);
	free(result1.ValueL);

	fprintM(C_TMP_DIR "mcLeastC\\x3.matrix",result3.x,n,k);
	fprintM(C_TMP_DIR "mcLeastC\\funVal3.list",result3.funVal,opt.maxIter,1);
	fprintM(C_TMP_DIR "mcLeastC\\lVal3.list",result3.ValueL,opt.maxIter,1);
	free(result3.x);
	free(result3.funVal);
	free(result3.ValueL);
}

